﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using RG.Common.Provider;
using RG.Consts;
using RG.DAO;
using RealtorGuide.DAO;

namespace RealtorGuide.Classes.Impl
{
    public class ManagerService : IManagerService
    {
        [Dependency]
        public IManagerDAO ManagerDAO { get; set; }


        [DAOTransaction]
        [RGTransaction]
        public Manager Get(int id)
        {
            return new Manager().CopyFrom(ManagerDAO.Get(id));
        }

        [DAOTransaction]
        [RGTransaction]
        public Manager Put(Manager item)
        {
            var dao = item.Id > 0 ? ManagerDAO.Get(item.Id) : new ManagerDAOObj();

            dao = dao.CopyFrom(item);

            if (dao.Id == 0)
                ManagerDAO.Put(dao);

            return new Manager().CopyFrom(dao);
        }

        [DAOTransaction]
        [RGTransaction]
        public List<Manager> List(ManagersQueryParams param)
        {
            return ManagerDAO.List(param).Select(x => new Manager().CopyFrom(x)).ToList();
        }

        [DAOTransaction]
        [RGTransaction]
        public List<int> ListPrepare(ManagersQueryParams param)
        {
            return ManagerDAO.List(param).Select(x => x.Id).ToList();
        }
    }

    public static class IManagerExtension
    {
        public static T CopyFrom<T>(this T dest,IManager source)
            where T : IManager
        {
            dest.Id = source.Id;
            dest.FIO = source.FIO;
            dest.Notes = source.Notes;
            dest.Phone = source.Phone;
            return dest;
        }
    }
}
